package com.inforich.web.controller.orderManage;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.carManage.domain.Driver;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.orderManage.domain.Booking;
import com.inforich.orderManage.service.IBookingService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * 预约Controller
 *
 * @author hjm
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/orderManage/booking")
public class BookingController extends BaseController {

  @Autowired
  private IBookingService bookingService;

  /**
   * 查询预约列表
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:list')")
  @GetMapping("/list")
  public Page<Booking> list(Page<Booking> page, Booking booking) {
    Map<String, Object> map = beanToMap(booking);
    bookingService.pageBooking(page, map);
    page.getRecords().forEach(r -> r.setDriverNames(getDriverName(r.getDrivers())));
    return page;
  }


  /**
   * 导出预约列表
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:export')")
  @Log(title = "预约", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, Booking booking) {
    Map<String, Object> map = beanToMap(booking);
    List<Booking> list = bookingService.listBooking(map);
    list.forEach(r -> r.setDriverNames(getDriverName(r.getDrivers())));
    ExcelUtil<Booking> util = new ExcelUtil<Booking>(Booking.class);
    util.exportExcel(response, list, "预约数据");
  }

  /**
   * 获取预约详细信息
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") String id) {
    return AjaxResult.success(bookingService.getBookingById(id));
  }

  /**
   * 新增预约
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:add')")
  @Log(title = "预约", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Booking booking) {
    return toAjax(bookingService.save(booking));
  }

  /**
   * 修改预约
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:edit')")
  @Log(title = "修改预约", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody Booking booking) {
    return toAjax(bookingService.updateById(booking));
  }

  /**
   * 删除预约
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:remove')")
  @Log(title = "删除预约", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable String[] ids) {
    return toAjax(bookingService.removeByIds(Arrays.asList(ids)));
  }

  /**
   * 结束预约单
   */
  @PreAuthorize("@ss.hasPermi('orderManage:booking:complete')")
  @GetMapping(value = "/complete/{id}")
  public AjaxResult complete(@PathVariable("id") String id) {
    return bookingService.complete(id);
  }

  /**
   * 司机车牌号
   */
  public String getDriverName(List<Driver> drivers) {
    return CollUtil.isEmpty(drivers) ? ""
        : drivers.stream().map(Driver::getCarNumber).collect(Collectors.joining(","));
  }
}
